// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AudioManage, { soundList } from "../../framework/AudioManage";
import Tool from "../../framework/Tool";


const {ccclass, property} = cc._decorator;

export enum mainPrefabView {
    EasterEggWallLayer = 0, //彩蛋界面
    CompensationLayer = 1, //补偿界面
    LuckyPlaneLayer = 2,  //幸运转盘
    InteriorTestLayer = 3, //内测员
    CatchDuckInquiryLayer = 4, //进入抓鸭子询问
    EnterCatchDuckLayer = 5, //进入抓鸭子界面
    CreateTavernLayer = 6, //创建酒馆界面
    EnterTavernLayer = 7, //进入酒馆界面
    SpringHappyLayer = 8, //春节狂欢
    SettingLayer = 9,   //设置界面
    ChooseLevelRank = 10, //普通关卡第一阶段
    NormalLevelLayer_1 = 11, //普通关卡第二阶段
    NormalLevelLayer_2 = 12, //普通关卡第三阶段
    SpecialLevelLayer = 13, //特殊关卡
    PetListLayer = 14, //宠物关卡
    SkinListLayer = 15, //宠物关卡
    BackHomeLayer = 16, //返场界面
    EnterEasterEggLevelLayer = 17, //是否进入彩蛋界面
    UserAgreementLayer = 18, //是否进入彩蛋界面
    randomShowRoleSkinLayer = 19, //随机使用人物皮肤
    logoSevenDayLayer = 20,  //累计登录
}

@ccclass
export default class mainMediator extends cc.Component {

    @property(cc.Node)
    roleNode : cc.Node = null;

    @property(cc.SpriteFrame)
    picList : cc.SpriteFrame[] = [];

    static isLoading = false;


    onLoad () {
        //初始化bgm
        AudioManage.initBgmVolume();
    }

    TouchStart(event){
        this.node.getChildByName("level").scale = 1;
    }

    TouchEnd(event){
        this.node.getChildByName("level").scale = 0.9;
    }

    start(){

        this.roleNode.getChildByName("pic").active = false;

        if(!mainMediator.isLoading){
            mainMediator.isLoading = true;
            cc.loader.loadRes("sound/bg",cc.AudioClip,(completedCount: number, totalCount: number, item: any)=>{

            },(error, resource) => {
                if(error){
                    console.log("加载音频资源==>",error);
                    return;
                }
                console.log("=====>音频资源加载完毕<=====");
                AudioManage.playBGM(soundList.musicMenu);
            });
        }
    }

    onClick(event,data){
        if(parseInt(data) == 1){
            //设置界面
            this.showPrefabLayer(mainPrefabView.SettingLayer, ()=>{

            });
        }else{
            AudioManage.playSFX(soundList.uiClick,false);
            this.showPrefabLayer(mainPrefabView.ChooseLevelRank, ()=>{

            });
        }
    }

    //展示预制界面
    showPrefabLayer(layer : mainPrefabView,callback){
        switch (layer) {
            //返场界面---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.BackHomeLayer :
                Tool.loadPrefab("mainPrefab/skinBackLayer",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;
            //彩蛋墙界面---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.EasterEggWallLayer :
                Tool.loadPrefab("mainPrefab/eggWall",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("eggWallMediator").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;

            //彩蛋界面---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.EnterEasterEggLevelLayer :
                Tool.loadPrefab("mainPrefab/easterEggView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("easterEggWindow").setShowInit(1,()=>{
                        callback(true)
                    },-1,()=>{
                        callback(false)
                    });
                    view.parent = cc.find("Canvas");
                });
                break;

            //补偿界面---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.CompensationLayer :
                Tool.loadPrefab("prefabs/compensation",res=>{
                    let view = cc.instantiate(res);
                    view.getComponent("compensationMediator").setCallBack(true,()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            //幸运转盘---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.LuckyPlaneLayer :
                AudioManage.stopBGM();
                Tool.loadPrefab("mainPrefab/turnplateView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("turnplateView").setCallback(()=>{
                        AudioManage.playBGM(soundList.musicMenu);
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            //抓鸭子询问---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.CatchDuckInquiryLayer :
                Tool.loadPrefab("mainPrefab/enterDuckLayer",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;
            //创建酒馆询问---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.CreateTavernLayer :
                Tool.loadPrefab("mainPrefab/createTavernLayer",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;
            //春节狂欢---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.SpringHappyLayer :
                Tool.loadPrefab("mainPrefab/newYearDay",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("newYearDay").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                callback();
                break;
            //进入内测员---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.InteriorTestLayer :
                AudioManage.stopBGM();

                break;
            //宠物界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.PetListLayer :
                Tool.loadPrefab("mainPrefab/petView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("petViewMediator").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            //皮肤界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.SkinListLayer :
                Tool.loadPrefab("mainPrefab/skinView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("skinViewMediator").setCloseBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            //设置界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.SettingLayer :
                Tool.loadPrefab("mainPrefab/settingView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("settingView").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            //普通关卡界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.ChooseLevelRank :

                Tool.loadPrefab("mainPrefab/chooseLevelRank",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("chooseLevelRank").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
                //普通关卡界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.NormalLevelLayer_1 :
                Tool.loadPrefab("mainPrefab/levelView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("levelViewMediator").setCallBack(1,()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
                //普通关卡界面 ---------------------------------------------------------------------------》》》--------------------------------------
            case mainPrefabView.NormalLevelLayer_2 :
                Tool.loadPrefab("mainPrefab/levelView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("levelViewMediator").setCallBack(2,()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            case mainPrefabView.SpecialLevelLayer :
                Tool.loadPrefab("mainPrefab/princeLevelView",(res)=>{
                    let view = cc.instantiate(res);
                    view.getComponent("princeLevelViewMediator").setCallBack(()=>{
                        callback();
                    });
                    view.parent = cc.find("Canvas");
                });
                break;
            case mainPrefabView.UserAgreementLayer :
                Tool.loadPrefab("mainPrefab/userAgreement",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;
            case mainPrefabView.randomShowRoleSkinLayer :
                Tool.loadPrefab("mainPrefab/randomShowRoleSkin",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;

            case mainPrefabView.logoSevenDayLayer :
                Tool.loadPrefab("mainPrefab/logoSevenDayLayer",(res)=>{
                    let view = cc.instantiate(res);
                    view.parent = cc.find("Canvas");
                    callback();
                });
                break;
            default:
                break;

        }
    }

    // update (dt) {}
}
